use axum::{Router, Json};
use axum::extract::Query;
use axum::routing::get;
use serde::{Serialize, Deserialize};

use crate::models::{Comment, Guide, ImageShare};

use super::protected::image_share;
// use tower_cookies::Cookies;

// use crate::models::User;
// use crate::utils::cookie;

type RouterResult<T> = Result<T,String>;


// struct RouterResponse {

// }

// #[derive(Serialize,Deserialize)]
// struct DataResponse<T>{
//     pub status: &'static str,
//     pub data: Vec<T>,
// }
//
// impl<T> DataResponse<T> {
//     pub fn new(status:&'static str,data: Vec<T>) -> DataResponse<T> {
//         DataResponse{
//             status: status,
//             data: data
//         }
//     }
// }

pub fn router() -> Router {
    Router::new()
        .route("/", get(index))
        .route("/get_all", get(get_all))
        .route("/recommendation", get(recommendation))
        .route("/get_all_comments",get(get_all_comments))
}

async fn index() -> RouterResult<String> {
    Ok("欢迎来到论坛页面！".to_string())
}


async fn get_all() -> Json<Vec<ImageShare>> {
    // let guides = Guide::fetch_all().await.unwrap();

    let image_shares = ImageShare::fetch_all().await.unwrap();



    Json(image_shares)

}
#[derive(Serialize,Deserialize,Debug)]
struct GetImageShareComment{
    image_share_id:i32
}

/// 获取美图的所有评论
async fn get_all_comments(Query(query):Query<GetImageShareComment>) -> Json<Vec<Comment>> {
    let comments = ImageShare::fetch_comments_of(    query.image_share_id).await.unwrap();
    Json(comments)

}

async fn recommendation() -> Json<Vec<ImageShare>> {
    // let guides = Guide::fetch_all().await.unwrap();

    // let image_shares = ImageShare::fetch_all().await.unwrap();
    let image_shares = ImageShare::fetch_recommendation().await.unwrap();


    // let rs = DataResponse::new("ok",image_shares);

    Json(image_shares)

}

